package com.xbjtech.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.xbjtech.model.Resource;
import com.xbjtech.model.User;
import com.xbjtech.model.UserExample;
import com.xbjtech.model.UserExample.Criteria;
import com.xbjtech.service.UserService;
import com.xbjtech.util.ResponseBean;

import lombok.extern.slf4j.Slf4j;

/**
* @author sparkCat E-mail:2925365275@qq.com
* @version 创建时间：2019年2月11日 下午2:44:02
* 类说明
*/
@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {

	@Autowired
	UserService userService;
	
	@RequestMapping("/list")
	public String list() {
		return "user/list";
	}
	
	@RequestMapping("/page")
	@ResponseBody
	public Map<String, Object> page(
			User user,
			@RequestParam(required = false, defaultValue = "1") Integer page, 
			@RequestParam(required = false, defaultValue = "10")Integer rows) {
		log.info("user:{}", user);
		log.error("user.deleted:{}",user.getDeleted());
		UserExample example = new UserExample();
		example.setOrderByClause("id desc");
		Criteria criteria = example.createCriteria();
		if (null != user.getUsername() && !user.getUsername().isEmpty()) {
			criteria.andUsernameLike("%"+user.getUsername()+"%");
		}
		if (null != user.getName() && !user.getName().isEmpty()) {
			criteria.andNameLike("%"+user.getName()+"%");
		}
		if (null != user.getMobile() && !user.getMobile().isEmpty()) {
			criteria.andMobileLike("%"+user.getMobile()+"%");
		}
		if (null != user.getLocked()) {
			criteria.andLockedEqualTo(user.getLocked());
		}
		if (null != user.getDeleted()) {
			criteria.andDeletedEqualTo(user.getDeleted());
		}
		List<User> list = userService.listUsers(example, page, rows);
		PageInfo<User> pageInfo = new PageInfo<>(list);
		Map<String, Object> map = new HashMap<>();
		map.put("total", pageInfo.getTotal());
		map.put("rows", pageInfo.getList());
		return map;
	}
	
	@PostMapping("/save")
	@ResponseBody
	public ResponseBean save(User user) {
		log.info("user:{}", user);
		try {
			if (user.getId() == null) {
				userService.insertUser(user);;
				return new ResponseBean(true, "保存成功");
			} else {
				userService.updateUser(user);;
				return new ResponseBean(true, "更新成功");
			}
		} catch (Exception e) {
			log.error("UserController.save() throws a exception -> {}", e.getMessage());
		}
		return new ResponseBean(false, "操作失败");
	}
	
	@PostMapping("/delete")
	@ResponseBody
	public ResponseBean delete(Long id) {
		try {
			userService.deleteUserById(id);
			return new ResponseBean(true, "删除成功");
		} catch (Exception e) {
			log.error("{}", e.getMessage());
		}
		return new ResponseBean(false, "删除失败");
	}
}
